package cn.liuhuan1.blogsModel.controller;

import cn.liuhuan1.api.FastDfsClient;
import cn.liuhuan1.entity.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Auther: liuhuan
 * @Date: 2021/4/10 - 04 - 10 - 18:31
 * @Description:
 * @version: 1.0
 */
@Controller
@RequestMapping("/fastDfs")
public class FastDfsController {

    @Autowired
    private FastDfsClient fastDfsClient;

    @RequestMapping(value = "/uploadUrl",method = RequestMethod.POST)
    @ResponseBody
    public String uploadUrl(@RequestPart("file") MultipartFile multipartFile){
        System.out.println("dd"+multipartFile.getOriginalFilename());
        try {
            return fastDfsClient.upload(multipartFile.getBytes(),multipartFile.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    @RequestMapping(value = "/delFileUrl",method = RequestMethod.POST)
    @ResponseBody
    public Integer delFileUrl(@RequestParam("urls") String urls){
        int index = urls.indexOf("/");
        String group = urls.substring(0, index);
        String url = urls.substring(index + 1);
        Integer is = fastDfsClient.delFileUrl(group,url);
        return is;
    }
}
